﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace OnWork.Admin.Util.Base
{
    /// <summary>
    ///过滤器 结果统一返回
    /// </summary>
    public class ApiResultFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);
        }

        public override void OnResultExecuting(ResultExecutingContext context)
        {
            var objectResult = context.Result as ObjectResult;
            //如果是通用对象直接返回 不是则包一层
            if (objectResult != null && objectResult.Value is ReturnModel)
            {
                context.Result = new OkObjectResult(objectResult.Value);
            }
            else
            {
                context.Result = new OkObjectResult(new ReturnModel(code: 200, data: objectResult.Value) { Msg = "获取成功!", Ok = true });
            }
        }
    }
}